package com.geek.water.exception;

import com.geek.water.constant.CodeConstant;
import com.geek.water.constant.MessageConstant;
import com.geek.water.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author yangwenqi
 * @decs 全局异常处理器
 * @date 2025/7/29
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(WaterException.class)
    public Result handleWaterException(WaterException e) {
        log.warn("业务异常: {}", e.getMessage());
        return new Result(CodeConstant.FAIL, e.getMessage());
    }

    @ExceptionHandler(BindException.class)
    public Result handleBindException(BindException e) {
        log.warn("参数绑定异常: {}", e.getMessage());
        // 处理areaId类型转换错误
        if (e.getMessage().contains("areaId")) {
            return new Result(CodeConstant.FAIL, "区域ID格式错误，请重新选择区域");
        }
        return new Result(CodeConstant.FAIL, "参数格式错误: " + e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        log.error("系统异常", e);
        return new Result(CodeConstant.FAIL, MessageConstant.INTERNAL_ERROR);
    }
}